package cn.com.dycc.study_manage.integral.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import cn.com.dycc.common.log.Constants;
import cn.com.dycc.common.log.SystemServiceLog;
import cn.com.dycc.study_manage.integral.dao.Pb_Integral_RuleDao;
import cn.com.dycc.study_manage.integral.entity.Pb_Integral_Rule;
import cn.com.dycc.study_manage.integral.param.Pb_Integral_RuleParam;

import cn.com.dycc.util.ConfigRegex;
import cn.com.dycc.util.DateUtil;

import cn.com.dycc.util.JsonObjectResult;
import cn.com.dycc.util.PageObjectUtil;
import cn.com.dycc.util.StringUtil;


@Service("pb_Integral_RuleService")
@Transactional
public class Pb_Integral_RuleServiceImpl implements Pb_Integral_RuleService{

	@Resource
	private Pb_Integral_RuleDao pb_Integral_RuleDao;
	
	
	@SystemServiceLog(description=Constants.INTEGRAL_RULE_ADD)
	public int addPb_Integral_Rule(Pb_Integral_RuleParam param, HttpServletRequest request)
			throws Pb_Integral_RuleNotFoundException {
		checkParam(param);
		HttpSession session=request.getSession();
		String opt_user_id=(String)session.getAttribute("id");
		String create_date=DateUtil.getNowTimestamp().toString();
		String org_id=(String)session.getAttribute("org_id");
		
		Pb_Integral_RuleParam param2=new Pb_Integral_RuleParam();
		param2.setOrg_id(org_id);
		String count=pb_Integral_RuleDao.getPb_Integral_RuleCountByParam(param2);
		if(!"0".equals(count)){
			throw new Pb_Integral_RuleNotFoundException("积分规则已存在不能重复添加");
		}
		StringBuilder builder=new StringBuilder();
		if(param.getActivity_send_val()==null||param.getActivity_send_val().trim().isEmpty()){
			builder.append("专题活动类（发贴）积分值不能为空");
		}
		if(param.getActivity_reply_val()==null||param.getActivity_reply_val().trim().isEmpty()){
			builder.append("专题活动类（回贴）积分值不能为空");
		}
		if(param.getLearn_send_val()==null||param.getLearn_send_val().trim().isEmpty()){
			builder.append("专题活动类（回贴）积分值不能为空");
		}
		if(param.getLearn_reply_val()==null||param.getLearn_reply_val().trim().isEmpty()){
			builder.append("教育、学习类（回贴）积分值不能为空");
		}
		if(param.getTask_send_val()==null||param.getTask_send_val().trim().isEmpty()){
			builder.append("任务类(发贴）积分值不能为空");
		}
		if(param.getTask_reply_val()==null||param.getTask_reply_val().trim().isEmpty()){
			builder.append("任务类（回贴）积分值不能为空");
		}
		if(!"".equals(builder.toString())){
			throw new Pb_Integral_RuleNotFoundException(builder.toString());
		}
		
		param.setInfo_id("1");
		param.setOpt_user_id(opt_user_id);
		param.setCreate_date(create_date);
		param.setState("Y");
		param.setOrg_id(org_id);
		List<Pb_Integral_Rule> lists = getServiceList(param);
		Pb_Integral_Rule entity=lists.get(0);
		int d = pb_Integral_RuleDao.savePb_Integral_Rule(entity);
		
		
		return d;
		
	}

	

	@SystemServiceLog(description=Constants.INTEGRAL_RULE_UPDATE)
	public int updatePb_Integral_Rule(Pb_Integral_RuleParam param, HttpServletRequest request)
			throws Pb_Integral_RuleNotFoundException {
		checkParam(param);
		HttpSession session=request.getSession();
		String org_id=(String)session.getAttribute("org_id");
		param.setOrg_id(org_id);
		if (param.getInfo_id() == null || param.getInfo_id().trim().isEmpty()) {
			throw new Pb_Integral_RuleNotFoundException("请选择要修改的积分规则信息");
		}
		
		List<Pb_Integral_Rule> lists = getServiceList(param);
		int d = pb_Integral_RuleDao.updatePb_Integral_Rule(lists);
		
		return d;
	}

	public JsonObjectResult findPb_Integral_RuleByParam(Pb_Integral_RuleParam param,
			HttpServletRequest request) throws Pb_Integral_RuleNotFoundException {
		List<Pb_Integral_Rule> lists = new ArrayList<Pb_Integral_Rule>();
		
		checkParam(param);
		HttpSession session=request.getSession();
		String org_id=(String)session.getAttribute("org_id");
		param.setOrg_id(org_id);
		param = realParam(param, request);
		System.out.println("param:"+param);
		String count = pb_Integral_RuleDao.getPb_Integral_RuleCountByParam(param);
		
		if ("0".equals(count)) {			
			return new JsonObjectResult(lists);
		}
		param = (Pb_Integral_RuleParam) PageObjectUtil.getSqlPageObject(count, param);
		lists = pb_Integral_RuleDao.findPb_Integral_RuleByParam(param);
		
		
		return new JsonObjectResult(count,lists);
	}
	
	/**
	 * 实际参数
	 * 
	 * @param param
	 * @param request
	 * @return
	 */
	public Pb_Integral_RuleParam realParam(Pb_Integral_RuleParam param, HttpServletRequest request) {
		
		return param;
	}
	
	/**
	 * 校验 @param param @return @throws
	 */
	public void checkParam(Pb_Integral_RuleParam param) {
		if (param == null) {
			throw new Pb_Integral_RuleNotFoundException("未登录或超时,请重新登录");
		}
		
		StringBuilder builder=new StringBuilder();
		if(param.getActivity_send_val()!=null&&!param.getActivity_send_val().trim().isEmpty()){
			if(!param.getActivity_send_val().matches(ConfigRegex.numberRegex)){
				builder.append("专题活动类（发贴）积分值格式不对,应为正整数");
			}
			
		}
		if(param.getActivity_reply_val()!=null&&!param.getActivity_reply_val().trim().isEmpty()){
			if(!param.getActivity_reply_val().matches(ConfigRegex.numberRegex)){
				builder.append("专题活动类（回贴）积分值格式不对,应为正整数");
			}
			
		}
		if(param.getLearn_send_val()!=null&&!param.getLearn_send_val().trim().isEmpty()){
			if(!param.getLearn_send_val().matches(ConfigRegex.numberRegex)){
				builder.append("专题活动类（回贴）积分值格式不对,应为正整数");
			}
			
		}
		if(param.getLearn_reply_val()!=null&&!param.getLearn_reply_val().trim().isEmpty()){
			if(!param.getLearn_reply_val().matches(ConfigRegex.numberRegex)){
				builder.append("教育、学习类（回贴）积分值格式不对,应为正整数");
			}
			
		}
		if(param.getTask_send_val()!=null&&!param.getTask_send_val().trim().isEmpty()){
			if(!param.getTask_send_val().matches(ConfigRegex.numberRegex)){
				builder.append("任务类(发贴）积分值格式不对,应为正整数");
			}
			
		}
		if(param.getTask_reply_val()!=null&&!param.getTask_reply_val().trim().isEmpty()){
			if(!param.getTask_reply_val().matches(ConfigRegex.numberRegex)){
				builder.append("任务类（回贴）积分值格式不对,应为正整数");
			}
			
		}
		if(!"".equals(builder.toString())){
			throw new Pb_Integral_RuleNotFoundException(builder.toString());
		}

	}

	public List<Pb_Integral_Rule> getServiceList(Pb_Integral_RuleParam param) {
		Pb_Integral_Rule entity = new Pb_Integral_Rule();
		System.out.println(param);
		entity.setInfo_id(param.getInfo_id());
		entity.setActivity_send_val(StringUtil.format_StringToInteger(param.getActivity_send_val()));
		entity.setActivity_reply_val(StringUtil.format_StringToInteger(param.getActivity_reply_val()));
		entity.setLearn_send_val(StringUtil.format_StringToInteger(param.getLearn_send_val()));
		entity.setLearn_reply_val(StringUtil.format_StringToInteger(param.getLearn_reply_val()));
		entity.setTask_send_val(StringUtil.format_StringToInteger(param.getTask_send_val()));
		entity.setTask_reply_val(StringUtil.format_StringToInteger(param.getTask_reply_val()));
		
		entity.setState(param.getState());
		entity.setOrg_id(param.getOrg_id());
		if(param.getCreate_date()!=null&&!param.getCreate_date().trim().isEmpty()){
			entity.setCreate_date(Timestamp.valueOf(param.getCreate_date()));
		}
		entity.setOpt_user_id(param.getOpt_user_id());
		
		
		List<Pb_Integral_Rule> lists = new ArrayList<Pb_Integral_Rule>();
		lists.add(entity);
		return lists;
	}
	
	

}
